import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/data/repository/home_repository.dart';
import 'package:wu_you/l10n/messages.dart';
import 'package:wu_you/page/page_exchange.dart';
import 'package:wu_you/page/page_mall.dart';
import 'package:wu_you/page/page_market.dart';
import 'package:wu_you/page/page_mine.dart';
import 'package:wu_you/plugins/back_plugin.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/utils/utils.dart';
import 'package:wu_you/widgets/update_version_view.dart';

class MainPage extends StatefulWidget{
  @override
  MainPageState createState() => MainPageState();
}
class MainPageState extends State<MainPage> with TickerProviderStateMixin{
  TabController _controller;
  List<Widget> _pages = [];
  @override
  void initState() {
    super.initState();
    MainIndexCubit cubit = BlocProvider.of<MainIndexCubit>(context);
    if(cubit.state == 0){
      _checkVersion();
    }
    _pages
      ..add(MallPage())
      ..add(MarketPage())
      // ..add(PurchasePage())
      ..add(ExchangePage())
      ..add(MinePage());
    _controller = TabController(
        initialIndex: cubit.state, length: _pages.length, vsync: this);
    _controller.addListener(() {
      cubit.value = _controller.index;
      if(_controller.index == 0){
        _checkVersion();
      }
    });
    cubit.stream.listen((value) {
      _controller.index = value;
    });
  }

  //检查版本更新
  void _checkVersion() async {
    HomeRepository().getAppUpdateInfo().then((value) {
      if(value == null){
        return;
      }
      showDialog(
        context: context,
        barrierDismissible: true,
        builder: (context) {
          return UpdateVersionView(value);
        },
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context);
    return WillPopScope(
      onWillPop: _requestPop,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: TabBarView(
          physics: NeverScrollableScrollPhysics(),
          children: _pages,
          controller: _controller,
        ),
        bottomNavigationBar: BlocBuilder<MainIndexCubit, int>(
          builder: (context, state) {
            return BottomNavigationBar(
              items: _buildBottomItem(messages),
              currentIndex: state,
              iconSize: 20,
              onTap: onTabTapped,
              backgroundColor: Colors.white,
            );
          },
        ),
        // floatingActionButton: Container(
        //   height: 55,
        //   width: 55,
        //   padding: EdgeInsets.all(5),
        //   margin: EdgeInsets.only(top: 10),
        //   decoration: BoxDecoration(
        //       borderRadius: BorderRadius.circular(40), color: Colors.white),
        //   child:
        //   BlocBuilder<MainIndexCubit, int>(
        //       builder: (context, state) {
        //         return  FloatingActionButton(
        //           elevation: 0,
        //           backgroundColor: state == 2 ? HexColor("#FCEBEC") : HexColor("#F6F6F6"),
        //           child: Image.asset(state == 2 ? Utils.getImgPath("ic_purchase_select") : Utils.getImgPath('ic_purchase_unselect'),
        //             width: 22,
        //             height: 22,
        //           ),
        //           onPressed: () {
        //             _controller.index = 2;
        //           },
        //         );}),
        // ),
        // floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterDocked,
      ),
    );
  }
  Future<bool> _requestPop() {
    AndroidBackDesktop.backToDesktop();
    return Future.value(false);
  }


  void onTabTapped(int index) {
    _controller.index = index;
  }

  List<BottomNavigationBarItem> _buildBottomItem(messages) {
    return [
      BottomNavigationBarItem(
        icon: Padding(padding: EdgeInsets.only(top: 2),child: Image.asset(_controller.index == 0 ? Utils.getImgPath("ic_mall_select") : Utils.getImgPath("ic_mall_unselect"),width: 20,height: 20,)),
        label: messages['tabMall']
      ),
      BottomNavigationBarItem(
        icon: Padding(padding: EdgeInsets.only(top: 2),child: Image.asset(_controller.index == 1 ? Utils.getImgPath("ic_place_select") : Utils.getImgPath("ic_place_unselect"),width: 20,height: 20,)),
        label: messages['tabMarket'],
      ),
      // BottomNavigationBarItem(
      //   icon: Padding(padding: EdgeInsets.only(top: 2),child: Image.asset(_controller.index == 2 ? Utils.getImgPath("ic_purchase_select") : Utils.getImgPath("ic_purchase_unselect"),width: 22,height: 22,)),
      //   label: messages['tabProcure'],
      // ),
      BottomNavigationBarItem(
        icon: Padding(padding: EdgeInsets.only(top: 2),child: Image.asset(_controller.index == 2 ? Utils.getImgPath("ic_luck_select") : Utils.getImgPath("ic_luck_unselect"),width: 20,height: 20,)),
        label: messages['tabDraw'],
      ),
      BottomNavigationBarItem(
        icon: Padding(padding: EdgeInsets.only(top: 2),child: Image.asset(_controller.index == 3 ? Utils.getImgPath("ic_user_select") : Utils.getImgPath("ic_user_unselect"),width: 20,height: 20,)),
        label: messages['tabUser'],
      )
    ];
  }

}

class MainIndexCubit extends Cubit<int> {
  MainIndexCubit({int value = 0}) : super(value);

  set value(int value) {
    emit(value);
  }
}